home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / ui / drawing / example / CoordinatesDemo.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  3.3 KB  |  115 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.awt.*;
  18. import java.applet.Applet;
  19.  
  20. /* 
  21.  * This displays a framed area.  When the user clicks within
  22.  * the area, this program displays a dot and a string indicating
  23.  * the coordinates where the click occurred.
  24.  */
  25.  
  26. public class CoordinatesDemo extends Applet {
  27.     FramedArea framedArea;
  28.     Label label;
  29.  
  30.     public void init() {
  31.         GridBagLayout gridBag = new GridBagLayout();
  32.         GridBagConstraints c = new GridBagConstraints();
  33.  
  34.         setLayout(gridBag);
  35.  
  36.         framedArea = new FramedArea(this);
  37.         c.fill = GridBagConstraints.BOTH;
  38.         c.weighty = 1.0;
  39.         c.gridwidth = GridBagConstraints.REMAINDER; //end row
  40.         gridBag.setConstraints(framedArea, c);
  41.         add(framedArea);
  42.  
  43.         label = new Label("Click within the framed area.");
  44.         c.fill = GridBagConstraints.HORIZONTAL;
  45.         c.weightx = 1.0;
  46.         c.weighty = 0.0;
  47.         gridBag.setConstraints(label, c);
  48.         add(label);
  49.  
  50.         validate();
  51.     }
  52.  
  53.     public void coordsChanged(Point point) {
  54.         label.setText("Click occurred at coordinate ("
  55.                       + point.x + ", " + point.y + ").");
  56.         repaint();
  57.     }
  58. }
  59.  
  60. /* This class exists solely to put a frame around the coordinate area. */
  61. class FramedArea extends Panel {
  62.     public FramedArea(CoordinatesDemo controller) {
  63.         super();
  64.  
  65.         //Set layout to one that makes its contents as big as possible.
  66.         setLayout(new GridLayout(1,0));
  67.  
  68.         add(new CoordinateArea(controller));
  69.         validate();
  70.     }
  71.  
  72.     public Insets insets() {
  73.         return new Insets(4,4,5,5);
  74.     }
  75.  
  76.     public void paint(Graphics g) {
  77.         Dimension d = size();
  78.         Color bg = getBackground();
  79.  
  80.         g.setColor(bg);
  81.         g.draw3DRect(0, 0, d.width - 1, d.height - 1, true);
  82.         g.draw3DRect(3, 3, d.width - 7, d.height - 7, false);
  83.     }
  84. }
  85.  
  86. class CoordinateArea extends Canvas {
  87.     Point point = null;
  88.     CoordinatesDemo controller;
  89.  
  90.     public CoordinateArea(CoordinatesDemo controller) {
  91.         super();
  92.         this.controller = controller;
  93.     }
  94.  
  95.     public boolean mouseDown(Event event, int x, int y) {
  96.         if (point == null) {
  97.             point = new Point(x, y);
  98.         } else {
  99.             point.x = x;
  100.             point.y = y;
  101.         }
  102.         controller.coordsChanged(point);
  103.         repaint();
  104.  
  105.         return false;
  106.     }
  107.  
  108.     public void paint(Graphics g) {
  109.         //If user has chosen a point, paint a tiny rectangle on top.
  110.         if (point != null) {
  111.             g.fillRect(point.x - 1, point.y - 1, 2, 2);
  112.         }
  113.     }
  114. }
  115.